package beans;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import conversions.Conversions;

import model.ServerLogEntry;

import dao.HistoryDao;
import dto.DateDTO;
import dto.LogTypesDTO;
import dto.ServerLogEntryDTO;
import enumerations.LogType;


/**
 * @author zsombor
 * Session Bean implementation class LogSearchBean
 */

@Stateless
@LocalBean
public class LogSearchBean implements LogSearchBeanRemote {
	
	@EJB
	HistoryDao dao;
	
	@EJB
	Conversions conv;

	/**
	 * Default constructor.
	 */
	public LogSearchBean() {
		// TODO Auto-generated constructor stub
	}

	/**
	 *  Description: generates the query to get log entries by type
	 
	 * @author Zsombor
	 * @param  types  The class containing the search types defined by the user
	 * @return      the dynamic query generated to search the database
	 */
	
	@Override
	public String printTypes(LogTypesDTO types) {
		String query = new String();

		if (types.getTypes().length != 0) {
			query = "Select s from ServerLogEntry s where s.logType IN :types";

		}
		return query;
	}

	/**
	 *  Description: returns server log entries between the dates
	 
	 * @author Zsombor
	 * @param  start  The date to start the search from
	 * @param  end	The end date for the search
	 * @return     The list of server log entries between the given parameters
	 */
	
	public List<ServerLogEntryDTO> queryByDate(DateDTO start, DateDTO end) {

		

		List<ServerLogEntry> resultInt = new ArrayList<ServerLogEntry>();
		resultInt = dao.getQueryByDate(start, end);
		
		List<ServerLogEntryDTO> result = new ArrayList<ServerLogEntryDTO>();
		
		result = conv.serverLogsToDTOs(resultInt);
		
		return result;
	}
	
	/**
	 *  Description: returns server log entries by selected types
	 
	 * @author Zsombor
	 * @param  types  The class containing the search types defined by the user
	 * @return       The list of server log entries between the given parameters
	 */

	@Override
	public List<ServerLogEntryDTO> queryEntries(LogTypesDTO types) {
		List<String> typesres;
		

		String query = printTypes(types);
			List<ServerLogEntry> resultInterim = new ArrayList<ServerLogEntry>();
			typesres = Arrays.asList(types.getTypes());
			List<ServerLogEntryDTO> result = new ArrayList<ServerLogEntryDTO>();
			
			List<LogType> typeResult = new ArrayList<LogType>();
			
			for(int i=0;i<typesres.size();i++){
				typeResult.add(i, LogType.valueOf(typesres.get(i)));
			}
			
			if(!typeResult.isEmpty()){
			
			resultInterim = dao.getQueryByType(query, typeResult);
			}
			
			result = conv.serverLogsToDTOs(resultInterim);
			
			return result;
	}
	
	/**
	 *  Description: returns server log entries by selected types
	 
	 * @author Zsombor
	 * @param  types  The class containing the search types defined by the user
	 * @return       The list of server log entries between the given parameters
	 */
	public List<ServerLogEntryDTO> queryAllEntries() {
		List<ServerLogEntry> resultInt = new ArrayList<ServerLogEntry>();
		List<LogType> typesres = new ArrayList<LogType>();
		
		
		
		String query = "Select s from ServerLogEntry s where s.logType IN :types";
		
		typesres = Arrays.asList(LogType.values()); 
		
		resultInt = dao.getAllTypes(query,typesres);
		
		List<ServerLogEntryDTO> result = new ArrayList<ServerLogEntryDTO>();
		
		result = conv.serverLogsToDTOs(resultInt);
		
		return result;
	}
}
